/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.taxigang.client.bookmark_bubble;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.taxigang.client.facebook.ConnectState;

public class BubbleFactory {

	private static final String BUBBLE_SRC = "http://www.taxigang.com/bubble/bookmark_bubble.js";

	public  void createBubble() {

		ScriptElement script = Document.get().createScriptElement();
		script.setSrc(BUBBLE_SRC);
		Document.get().getElementsByTagName("head").getItem(0).appendChild(script);
		addOnLoadHandler(script);


	}
	
	public static native void addOnLoadHandler(ScriptElement script)/*-{
	script.onload = function() {
   		@com.taxigang.client.bookmark_bubble.BubbleFactory::apiReady()();
	}

	}-*/;

	public static native void apiReady()/*-{

	$wnd.addEventListener('load', function() {
		  $wnd.setTimeout(function() {
		    var bubble = new google.bookmarkbubble.Bubble();

		    var parameter = 'bmb=1';

		    bubble.hasHashParameter = function() {
		      return $wnd.location.hash.indexOf(parameter) != -1;
		    };

		    bubble.setHashParameter = function() {
		      if (!this.hasHashParameter()) {
		        $wnd.location.hash += parameter;
		      }
		    };

		    bubble.getViewportHeight = function() {
		      $wnd.console.log('Example of how to override getViewportHeight.');
		      return $wnd.innerHeight;
		    };

		    bubble.getViewportScrollY = function() {
		      $wnd.console.log('Example of how to override getViewportScrollY.');
		      return $wnd.pageYOffset;
		    };

		    bubble.registerScrollHandler = function(handler) {
		      $wnd.console.log('Example of how to override registerScrollHandler.');
		      $wnd.addEventListener('scroll', handler, false);
		    };

		    bubble.deregisterScrollHandler = function(handler) {
		      $wnd.console.log('Example of how to override deregisterScrollHandler.');
		      $wnd.removeEventListener('scroll', handler, false);
		    };

		    bubble.showIfAllowed();
		  }, 1000);
		}, false);

	}-*/;

}

